<template>
  <div class="admin-layout">
    <!-- 侧边栏 -->
    <aside class="sidebar" :class="{ 'collapsed': sidebarCollapsed }">
      <div class="sidebar-header">
        <div class="logo">
          <i class="fas fa-microchip"></i>
          <span v-if="!sidebarCollapsed" class="logo-text">PCB-CSM</span>
        </div>
        <button 
          class="sidebar-toggle"
          @click="toggleSidebar"
          :title="sidebarCollapsed ? '展开菜单' : '收起菜单'"
        >
          <i :class="sidebarCollapsed ? 'fas fa-chevron-right' : 'fas fa-chevron-left'"></i>
        </button>
      </div>
      
      <nav class="sidebar-nav">
        <ul class="nav-list">
          <li class="nav-item">
            <router-link 
              to="/admin/dashboard" 
              class="nav-link"
              :class="{ active: $route.name === 'adminDashboard' }"
            >
              <i class="fas fa-tachometer-alt"></i>
              <span v-if="!sidebarCollapsed">仪表盘</span>
            </router-link>
          </li>
          <li class="nav-item">
            <router-link 
              to="/admin/users" 
              class="nav-link"
              :class="{ active: $route.name === 'adminUsers' }"
            >
              <i class="fas fa-users"></i>
              <span v-if="!sidebarCollapsed">用户管理</span>
            </router-link>
          </li>
          <li class="nav-item">
            <router-link 
              to="/admin/categories" 
              class="nav-link"
              :class="{ active: $route.name === 'adminCategories' }"
            >
              <i class="fas fa-tags"></i>
              <span v-if="!sidebarCollapsed">分类管理</span>
            </router-link>
          </li>
          <li class="nav-item">
            <router-link 
              to="/admin/products" 
              class="nav-link"
              :class="{ active: $route.name === 'adminProducts' }"
            >
              <i class="fas fa-box"></i>
              <span v-if="!sidebarCollapsed">产品管理</span>
            </router-link>
          </li>
          <li class="nav-item">
            <router-link 
              to="/admin/orders" 
              class="nav-link"
              :class="{ active: $route.name === 'adminOrders' }"
            >
              <i class="fas fa-shopping-cart"></i>
              <span v-if="!sidebarCollapsed">订单管理</span>
            </router-link>
          </li>
        </ul>
      </nav>
    </aside>
    
    <!-- 主内容区域 -->
    <div class="main-content">
      <!-- 顶部头部 -->
      <header class="header">
        <div class="header-left">
          <div class="breadcrumb">
            <span class="breadcrumb-item">{{ currentPageTitle }}</span>
          </div>
        </div>
        
        <div class="header-right">
          <div class="admin-info">
            <div class="admin-avatar">
              <i class="fas fa-user-shield"></i>
            </div>
            <div v-if="currentAdmin" class="admin-details">
              <span class="admin-name">{{ currentAdmin.username }}</span>
              <span class="admin-role">管理员</span>
            </div>
            <div class="admin-actions">
              <button 
                class="action-button"
                @click="showLogoutConfirm = true"
                title="退出登录"
              >
                <i class="fas fa-sign-out-alt"></i>
              </button>
            </div>
          </div>
        </div>
      </header>
      
      <!-- 页面内容 -->
      <main class="page-content">
        <router-view />
      </main>
    </div>
    
    <!-- 退出登录确认对话框 -->
    <div v-if="showLogoutConfirm" class="modal-overlay" @click="showLogoutConfirm = false">
      <div class="modal-content" @click.stop>
        <div class="modal-header">
          <h3>确认退出</h3>
          <button class="modal-close" @click="showLogoutConfirm = false">
            <i class="fas fa-times"></i>
          </button>
        </div>
        <div class="modal-body">
          <p>您确定要退出管理员账户吗？</p>
        </div>
        <div class="modal-footer">
          <button class="btn btn-secondary" @click="showLogoutConfirm = false">
            取消
          </button>
          <button class="btn btn-danger" @click="handleLogout">
            确认退出
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, computed, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useStore } from 'vuex'

export default {
  name: 'AdminLayout',
  setup() {
    const router = useRouter()
    const route = useRoute()
    const store = useStore()
    
    // 响应式数据
    const sidebarCollapsed = ref(false)
    const showLogoutConfirm = ref(false)
    
    // 计算属性
    const currentAdmin = computed(() => store.getters.currentAdmin)
    
    const currentPageTitle = computed(() => {
      const titleMap = {
        'adminDashboard': '仪表盘',
        'adminUsers': '用户管理',
        'adminCategories': '分类管理',
        'adminProducts': '产品管理',
        'adminOrders': '订单管理'
      }
      return titleMap[route.name] || '管理后台'
    })
    
    // 方法
    const toggleSidebar = () => {
      sidebarCollapsed.value = !sidebarCollapsed.value
      // 保存状态到localStorage
      localStorage.setItem('sidebarCollapsed', sidebarCollapsed.value.toString())
    }
    
    const handleLogout = () => {
      // 清除管理员状态
      store.dispatch('logoutAdmin')
      
      // 清除记住我的信息
      localStorage.removeItem('adminCredentials')
      
      // 跳转到登录页
      router.push('/admin/login')
      
      showLogoutConfirm.value = false
    }
    
    // 生命周期
    onMounted(() => {
      // 检查登录状态
      if (!store.getters.isAdminLoggedIn) {
        router.push('/admin/login')
        return
      }
      
      // 恢复侧边栏状态
      const savedSidebarState = localStorage.getItem('sidebarCollapsed')
      if (savedSidebarState) {
        sidebarCollapsed.value = savedSidebarState === 'true'
      }
    })
    
    return {
      sidebarCollapsed,
      showLogoutConfirm,
      currentAdmin,
      currentPageTitle,
      toggleSidebar,
      handleLogout
    }
  }
}
</script>

<style scoped>
.admin-layout {
  display: flex;
  min-height: 100vh;
  background: #f5f6fa;
}

/* 侧边栏样式 */
.sidebar {
  width: 260px;
  background: linear-gradient(180deg, #2c3e50 0%, #34495e 100%);
  color: white;
  transition: width 0.3s ease;
  position: fixed;
  height: 100vh;
  z-index: 1000;
  box-shadow: 2px 0 10px rgba(0, 0, 0, 0.1);
}

.sidebar.collapsed {
  width: 70px;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.logo {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 20px;
  font-weight: 700;
}

.logo i {
  color: #3498db;
  font-size: 24px;
}

.logo-text {
  white-space: nowrap;
  overflow: hidden;
}

.sidebar-toggle {
  background: none;
  border: none;
  color: white;
  cursor: pointer;
  padding: 8px;
  border-radius: 6px;
  transition: all 0.3s ease;
}

.sidebar-toggle:hover {
  background: rgba(255, 255, 255, 0.1);
}

.sidebar-nav {
  padding: 20px 0;
}

.nav-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.nav-item {
  margin-bottom: 8px;
}

.nav-link {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 20px;
  color: rgba(255, 255, 255, 0.8);
  text-decoration: none;
  transition: all 0.3s ease;
  border-left: 3px solid transparent;
}

.nav-link:hover {
  background: rgba(255, 255, 255, 0.1);
  color: white;
  border-left-color: #3498db;
}

.nav-link.active {
  background: rgba(52, 152, 219, 0.2);
  color: white;
  border-left-color: #3498db;
}

.nav-link i {
  font-size: 18px;
  width: 18px;
  text-align: center;
}

.nav-link span {
  white-space: nowrap;
  overflow: hidden;
}

/* 主内容区域样式 */
.main-content {
  flex: 1;
  margin-left: 260px;
  transition: margin-left 0.3s ease;
  display: flex;
  flex-direction: column;
}

.sidebar.collapsed + .main-content {
  margin-left: 70px;
}

.header {
  background: white;
  height: 70px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 30px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 0;
  z-index: 999;
}

.header-left {
  display: flex;
  align-items: center;
}

.breadcrumb {
  display: flex;
  align-items: center;
  gap: 8px;
}

.breadcrumb-item {
  font-size: 20px;
  font-weight: 600;
  color: #2c3e50;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 20px;
}

.admin-info {
  display: flex;
  align-items: center;
  gap: 12px;
}

.admin-avatar {
  width: 40px;
  height: 40px;
  background: linear-gradient(135deg, #3498db, #2980b9);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 16px;
}

.admin-details {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.admin-name {
  font-weight: 600;
  color: #2c3e50;
  font-size: 14px;
}

.admin-role {
  font-size: 12px;
  color: #7f8c8d;
}

.admin-actions {
  display: flex;
  gap: 8px;
}

.action-button {
  width: 36px;
  height: 36px;
  border: none;
  background: #ecf0f1;
  border-radius: 8px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s ease;
  color: #7f8c8d;
}

.action-button:hover {
  background: #e74c3c;
  color: white;
}

.page-content {
  flex: 1;
  padding: 30px;
  overflow-y: auto;
}

/* 模态框样式 */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10000;
  backdrop-filter: blur(5px);
}

.modal-content {
  background: white;
  border-radius: 12px;
  width: 400px;
  max-width: 90vw;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.2);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 24px;
  border-bottom: 1px solid #ecf0f1;
}

.modal-header h3 {
  margin: 0;
  color: #2c3e50;
  font-size: 18px;
}

.modal-close {
  background: none;
  border: none;
  font-size: 18px;
  cursor: pointer;
  color: #7f8c8d;
  padding: 4px;
  border-radius: 4px;
  transition: color 0.3s ease;
}

.modal-close:hover {
  color: #e74c3c;
}

.modal-body {
  padding: 24px;
}

.modal-body p {
  margin: 0;
  color: #7f8c8d;
  line-height: 1.6;
}

.modal-footer {
  display: flex;
  gap: 12px;
  justify-content: flex-end;
  padding: 20px 24px;
  border-top: 1px solid #ecf0f1;
}

.btn {
  padding: 10px 20px;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 500;
  transition: all 0.3s ease;
}

.btn-secondary {
  background: #ecf0f1;
  color: #7f8c8d;
}

.btn-secondary:hover {
  background: #d5dbdb;
}

.btn-danger {
  background: #e74c3c;
  color: white;
}

.btn-danger:hover {
  background: #c0392b;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .sidebar {
    width: 70px;
  }
  
  .main-content {
    margin-left: 70px;
  }
  
  .header {
    padding: 0 20px;
  }
  
  .page-content {
    padding: 20px;
  }
  
  .admin-details {
    display: none;
  }
}

/* Font Awesome 图标 */
@import url('https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css');
</style> 